<?php
/**
 * @title 872. 叶子相似的树
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root1
     * @param TreeNode $root2
     * @return Boolean
     */
    function leafSimilar($root1, $root2) {
        /**
         * @return
         */
        return $this->TreeLeaf($root1) == $this->TreeLeaf($root2);
    }

    /**
     * @title 返回树的所有叶子节点
     * @author start2004
     * @since 2024-01-10 Created
     *
     * @param TreeNode $root
     * @return Integer[]
     */
    function TreeLeaf($root){
        /**
         * @since 2024-01-10 叶子节点集合
         */
        $leafArray = [];

        /**
         * @since 2024-01-10 左右子树为空
         */
        if($root->left === null AND $root->right === null){
            $leafArray[] = $root->val;
        } else {
            /**
             * @since 2024-01-10 左子树
             */
            if($root->left !== null) {
                $leafArray = array_merge($leafArray, $this->TreeLeaf($root->left));
            } else {}

            /**
             * @since 2024-01-10 右子树
             */
            if($root->right !== null) {
                $leafArray = array_merge($leafArray, $this->TreeLeaf($root->right));
            } else {}
        }

        /**
         * @return Integer[]
         */
        return $leafArray;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.10_2.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
